package site.wanjiahao.message;


import lombok.Data;
import site.wanjiahao.message.request.*;
import site.wanjiahao.message.response.*;

import java.util.HashMap;
import java.util.Map;

/**
 * 公共抽象父类，消息类型
 */
@Data
public abstract class Message {

    private static final Map<Integer, Class<? extends Message>> map = new HashMap<>();

    protected static final int LOGIN_REQUEST_ID = 1;
    protected static final int LOGIN_RESPONSE_ID = 2;
    protected static final int CHART_REQUEST_ID = 3;
    protected static final int CHART_RESPONSE_ID = 4;
    protected static final int CREATE_GROUP_REQUEST_ID = 5;
    protected static final int CREATE_GROUP_RESPONSE_ID = 6;
    protected static final int GROUP_CHART_REQUEST_ID= 7;
    protected static final int GROUP_CHART_RESPONSE_ID= 8;
    protected static final int GROUP_LIST_USERS_REQUEST_ID = 9;
    protected static final int GROUP_LIST_USERS_RESPONSE_ID = 10;
    protected static final int GROUP_JOIN_REQUEST_ID = 11;
    protected static final int GROUP_JOIN_RESPONSE_ID = 12;
    protected static final int GROUP_EXIT_REQUEST_ID = 13;
    protected static final int GROUP_EXIT_RESPONSE_ID = 14;
    protected static final int GROUP_LIST_REQUEST_ID = 15;
    protected static final int GROUP_LIST_RESPONSE_ID = 16;
    protected static final int PING_MESSAGE_ID = 17;
    protected static final int PONG_MESSAGE_ID = 18;

    private Long sequenceId;

    static {
        map.put(LOGIN_REQUEST_ID, LoginRequestMessage.class);
        map.put(LOGIN_RESPONSE_ID, LoginResponseMessage.class);
        map.put(CHART_REQUEST_ID, ChartRequestMessage.class);
        map.put(CHART_RESPONSE_ID, ChartResponseMessage.class);
        map.put(CREATE_GROUP_REQUEST_ID, CreateGroupRequestMessage.class);
        map.put(CREATE_GROUP_RESPONSE_ID, CreateGroupResponseMessage.class);
        map.put(GROUP_CHART_REQUEST_ID, GroupChartRequestMessage.class);
        map.put(GROUP_CHART_RESPONSE_ID, GroupChartResponseMessage.class);
        map.put(GROUP_LIST_USERS_REQUEST_ID, ListGroupMembersRequestMessage.class);
        map.put(GROUP_LIST_USERS_RESPONSE_ID, ListGroupMembersResponseMessage.class);
        map.put(GROUP_JOIN_REQUEST_ID, JoinGroupRequestMessage.class);
        map.put(GROUP_JOIN_RESPONSE_ID, JoinGroupResponseMessage.class);
        map.put(GROUP_EXIT_REQUEST_ID, QuitGroupRequestMessage.class);
        map.put(GROUP_EXIT_RESPONSE_ID, QuitGroupResponseMessage.class);
        map.put(GROUP_LIST_REQUEST_ID, ListGroupsRequestMessage.class);
        map.put(GROUP_LIST_RESPONSE_ID, ListGroupsResponseMessage.class);
        map.put(PING_MESSAGE_ID, PingMessage.class);
        map.put(PONG_MESSAGE_ID, PongMessage.class);
    }

    public Message() {
        this.sequenceId = System.currentTimeMillis();
    }

    public static Class<? extends Message> getClazz(int key) {
        return map.get(key);
    }

    public abstract int getMessageType();

}
